﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using RoofRunner.GameComponents.Rendering;
using Microsoft.Xna.Framework.Content;

namespace RoofRunner.Actors.ActorComponents.Rendering
{
    public class LODComponent : ActorComponent, ILODObject
    {
        #region Fields
        private Dictionary<float, string> _distanceAndRessourcenames;
        #endregion // Fields


        #region Properties
        public LODDefinition LODDefinition { get; private set; }
        public ModelRenderComponent ConnectedModelRenderComponent { get; private set; }
        #endregion // Properties


        #region Constructors
        public LODComponent(ModelRenderComponent connectedModelRenderComponent)
        {
            this.LODDefinition = new LODDefinition();
            this.ConnectedModelRenderComponent = connectedModelRenderComponent;
        }
        public LODComponent(ModelRenderComponent connectedModelRenderComponent, Dictionary<float, string> distanceAndRessourcenames)
        {
            this.LODDefinition = new LODDefinition();
            this._distanceAndRessourcenames = distanceAndRessourcenames;
            this.ConnectedModelRenderComponent = connectedModelRenderComponent;
        }
        #endregion // Constructors


        #region Methods
        public override void LoadContent(ContentManager content)
        {
            if (this._distanceAndRessourcenames != null)
            {
                foreach (var x in this._distanceAndRessourcenames)
                {
                    this.LODDefinition.Add(x.Key, content.Load<Model>(x.Value));
                }
            }
            RoofRunnerGame.RenderWorld.LODSystem.AddLODObject(this);
            base.LoadContent(content);
        }

        public override void UnloadContent()
        {
            RoofRunnerGame.RenderWorld.LODSystem.RemoveLODObject(this);
            base.UnloadContent();
        }

        


        public float GetDistance(ref Vector3 cameraPosition)
        {
            return Vector3.Distance(cameraPosition, this.Parent.Position);
        }


        public void LODUpdate(LODLevel level)
        {
            if (this.ConnectedModelRenderComponent.Model != null) // Reset LOD-Model
                this.ConnectedModelRenderComponent.Model = level.Model;
        }
        #endregion // Methods
    }


}
